package com.example.es.util;

import com.example.es.entity.Person;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.junit.Test;

import java.io.IOException;
import java.util.Date;

/**
 * @description: 增删改索引
 * @author: ruoan
 * @date: 2020/10/11 11:57
 */
public class EsRestHighLevelClientTest2 {

    private RestHighLevelClient client= EsRestHighLevelClient.getClient();
    private final String index = "person";
    private final String type = "man";


    /**
     * 创建索引
     */
    @Test
    public void createIndex() throws IOException, InterruptedException {
        Settings.Builder settings = Settings.builder()
                .put("number_of_shards", 3)
                .put("number_of_replicas", 3);

        XContentBuilder mappings = JsonXContent.contentBuilder()
                .startObject()
                    .startObject("properties")
                        .startObject("name")
                            .field("type","keyword")
                        .endObject()
                        .startObject("age")
                            .field("type","integer")
                        .endObject()
                        .startObject("birthday")
                            .field("type","date")
                            .field("format","yyyy-MM-dd")
                        .endObject()
                    .endObject()
                .endObject();

        CreateIndexRequest createIndexRequest = new CreateIndexRequest(index).settings(settings).mapping(mappings);


        ActionListener<CreateIndexResponse> listener = new ActionListener<CreateIndexResponse>() {
            @Override
            public void onResponse(CreateIndexResponse createIndexResponse) {
                System.out.println("成功");
            }

            @Override
            public void onFailure(Exception e) {
                System.out.println("失败");
            }
        };
        client.indices().createAsync(createIndexRequest, RequestOptions.DEFAULT,listener);

        Thread.sleep(3000);
        System.out.println(createIndexRequest);




    }





    /**
     * 检查索引是否存在
     */
    @Test
    public void existIndex() throws IOException {

        GetIndexRequest getIndexRequest = new GetIndexRequest(index);
        //GetIndexRequest getIndexRequest = new GetIndexRequest("gogeg");
        boolean exists = client.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
        System.out.println(exists);


    }

    /**
     * 删除索引
     */
    @Test
    public void deleteIndex() throws IOException {

        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(index);
        AcknowledgedResponse delete = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);

        System.out.println(delete.isAcknowledged());


    }




}